package com.sskj.mine;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.MD5Util;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.util.CommonUtil;
import com.sskj.mine.http.HttpConfig;
import com.sskj.mine.presenter.BasePresenter;


/**
 * @author Hey
 * Create at  2019/05/01
 */
class BindMobilePresenter extends BasePresenter<BindMobileActivity> {

    public void bindMobile(String mobile, String code, String tpwd) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + (mobile.contains("@") ? HttpConfig.BIND_EMAIL : HttpConfig.BIND_MOBILE))
                .params("mobile", mobile) // 手机号
                .params("email", mobile)  // 邮箱号
                .params("code", code)  // 验证码
                .params("tpwd", CommonUtil.getFiveMd5Value(tpwd))
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getStatus() == HttpConfig.OK) {
                            mView.finish();
                        }
                    }

                    @Override
                    public void onError(Response<HttpData> response) {
                        super.onError(response);
                    }
                });
    }

    public void sendCode(String mobile) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SMS_SEND)
                .params("mobile", mobile)
                .params("type", 3)
                .execute(new JsonCallBack<HttpData>(this, true) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            mView.onAuthCodeSuccess();
                        }
                    }

                    @Override
                    public void onError(Response<HttpData> response) {
                        super.onError(response);
                    }
                });
    }

    // 发送邮箱验证码
    // 获取邮箱验证码
    public void sendEmail(String email) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SEND_EMAIL)
                .params("email", email)
                .execute(new JsonCallBack<HttpData>(this) {

                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        ToastUtil.showShort(response.body().getMsg());
                        mView.onAuthCodeSuccess();
                    }
                });
    }
}
